package com.xique.door.controller;

import com.xique.common.core.domain.R;
import com.xique.common.core.utils.AliossUtil;
import com.xique.common.core.utils.SecurityUtils;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.core.web.domain.PageResult;
import com.xique.common.log.annotation.Log;
import com.xique.common.log.enums.BusinessType;
import com.xique.common.security.annotation.PreAuthorize;
import com.xique.door.bean.domain.OssfileUrl;
import com.xique.door.bean.vo.device.DeviceScreenVO;
import com.xique.door.device.freeview.service.biz.FreeviewBizService;
import com.xique.door.service.IOssfileUrlService;
import com.xique.door.util.FileUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/** 固件升级 */
@RestController
@RequestMapping("/firmwareUpdate")
public class FirmwareUpdateController {

  @Autowired private IOssfileUrlService ossfileUrlService;

  @Autowired private AliossUtil aliossUtil;

  @Autowired private FreeviewBizService freeviewBizService;

  @PreAuthorize(hasPermi = "door:firmwareUpdate:add")
  @ResponseBody
  @PostMapping("/uploadPackage")
  @Log(title = "oss素材上传", businessType = BusinessType.IMPORT)
  @ApiOperation("oss素材上传")
  public AjaxResult uploadPackage(
      @RequestParam("file") MultipartFile file,
      Integer fileType,
      String title,
      String model,
      String type,
      String remark)
      throws Exception {
    if (file != null && fileType != null && title != null) {
      OssfileUrl oss = new OssfileUrl();
      oss.setFileType(fileType);
      oss.setTitle(title);
      oss.setModel(model);
      oss.setType(type);
      oss.setCreateBy(SecurityUtils.getUserId());
      oss.setUpdateBy(SecurityUtils.getUserId());
      oss.setRemark(remark);

      oss.setFilesize(FileUtil.FormetFileSize(file.getSize()));
      // 上传的文件
      ossfileUrlService.uploadMineVideo(file, oss);
      // 休眠3秒避免请求结束，文件取不到
      Thread.sleep(3000);
      System.out.println("文件上传中，请稍后");
      return AjaxResult.success("文件上传中，请稍后");
    } else {
      return AjaxResult.error("error", "文件上传失败！！！");
    }
  }

  /** 列表 */
  @PreAuthorize(hasPermi = "door:firmwareUpdate:list")
  @GetMapping("/list")
  public PageResult getFirmwarelList(
      Integer fileType,
      String title,
      Integer pageNum,
      Integer pageSize,
      String startTime,
      String endTime,
      String type,
      String model,String userName) {
    if (fileType == null) {
      fileType = 3;
    }
    return ossfileUrlService.getOssfileUrlList(
        fileType, title, pageNum, pageSize, startTime, endTime, type, model,userName);
  }

  /** 编辑 */
  @PreAuthorize(hasPermi = "door:firmwareUpdate:edit")
  @GetMapping("/modify")
  public R ModiFyfirmware(OssfileUrl ossfileUrl) {
    return ossfileUrlService.ModifyOssfileUr(ossfileUrl);
  }

  /** 详情 */
  @PreAuthorize(hasPermi = "door:firmwareUpdate:edit")
  @GetMapping("/firmwareUpdateById")
  public R getFirmwareById(OssfileUrl ossfileUrl) {
    return ossfileUrlService.getOssfileUrById(ossfileUrl);
  }

  @PreAuthorize(hasPermi = "door:firmwareUpdate:down")
  @ApiOperation(value = "下载安装包")
  @GetMapping(value = "/download")
  @ResponseBody
  public AjaxResult download(
      @RequestParam("fileName") String fileName, HttpServletResponse response) throws Exception {

    String status = aliossUtil.download(fileName, response);
    if (status.equals("error")) {
      return AjaxResult.error("error", "文件下载失败！！！");

    } else {
      return AjaxResult.error("success", "文件下载成功！！！");
    }
  }

  @PreAuthorize(hasPermi = "door:firmwareUpdate:upgrade")
  @Log(title = "门禁设备固件升级", businessType = BusinessType.UPDATE)
  @GetMapping("/upgrade")
  @ResponseBody
  public AjaxResult FreeviewUpgrade(
      String devSn, String filepath, String fileEquipmentModel, String model) throws Exception {
    AjaxResult ajaxResult = null;
    if (!fileEquipmentModel.equals(model)) {
      return AjaxResult.error("设备类型与文件类型不匹配");
    } else {
      String newUrl = filepath;
      newUrl = newUrl.split("[?]")[0];
      String[] bb = newUrl.split("/");
      // 得到最后一个分隔符后的名字
      String fileName = bb[bb.length - 1];

      MultipartFile cMultiFile = FileUtil.createFileItem(filepath, fileName);
      ajaxResult = freeviewBizService.freeviewUpgrade(devSn, cMultiFile,filepath);
    }
    return ajaxResult;
  }

  /** 固件升级 */
  // @PreAuthorize(hasPermi = "door:firmwareUpdate:Firmwarelist")
  @GetMapping("/firmwarelist")
  public PageResult getFirmwarelist(Integer pageNum, Integer pageSize, String model) {
    return ossfileUrlService.getOssfileUrlList(3, null, pageNum, pageSize, null, null, "2", model,null);
  }

  /** 设置屏息 */
  @ResponseBody
  @PostMapping("/screenSaveTimesRange")
  @Log(title = "设置屏幕息屏", businessType = BusinessType.COMMAND)
  @ApiOperation("设置屏幕息屏")
  public AjaxResult screenSaveTimesRange(@RequestBody DeviceScreenVO deviceScreenVO){
    return freeviewBizService.screenSaveTimesRange(deviceScreenVO.getDevSn(),deviceScreenVO.getRangeTimes());
  }
}
